package com.fishbone.sso.handler;

import com.fishbone.common.utils.FastJsonUtil;
import com.fishbone.common.utils.WebUtil;
import com.fishbone.core.api.ResponseEntity;
import com.fishbone.core.api.ResultCode;
import lombok.SneakyThrows;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 自定义Token验证失败处理器
 * @ClassName CustomAuthenticationEntryPoint
 * @Author huyaxi
 * @DATE 2020/3/5 16:23
 */
public class FishBoneAccessDeniedHandler implements AccessDeniedHandler {

    @SneakyThrows
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) {
        response.setStatus(HttpStatus.FORBIDDEN.value());
        WebUtil.response(response, FastJsonUtil.toJSONString(ResponseEntity.fail(ResultCode.REQ_REJECT)));
    }
}
